// 公共头部
import myHeader from "/pages/headerfoot/pages/header.vue";
//公共底部
import Tabbar from '/pages/headerfoot/pages/hitgamefooter.vue'
//表格
import uniForms from "/uni_modules/uni-forms/components/uni-forms/uni-forms.vue";
//登录api
import Login from '@/lib/login.js';
//用户
import User from '@/lib/user.js';
//cache缓存
import Cache from '@/utils/cache.js';
//打帮赛方法
import hitmatchcommon from '@/common/hitmatchcommon.js';
//基础
import Base from '@/common/base.js'

export default {
	components: {
		myHeader,
		Tabbar,
		uniForms
	},
	data() {
		return {
			imageValue: [], //图片
			tempFilePaths: [], //临时文件
			// 单选数据源
			sexs: [{
				text: '男',
				value: 1
			}, {
				text: '女',
				value: 2
			}],
			valiFormData: {
				name: '',
				age: '',
				sex: '',
				identitycard: '',
				nickname: '',
				phone: '',
				description: '',
			},
			// 1校验规则
			rules: {
				name: {
					rules: [{
						required: true,
						errorMessage: '姓名不能为空'
					}]
				},
				sex: {
					rules: [{
						required: true,
						errorMessage: '请选性别',
					}]
				},
				age: {
					rules: [{
						required: true,
						errorMessage: '年龄不能为空'
					}, {
						format: 'number',
						errorMessage: '年龄只能输入数字'
					}]
				},
				identitycard: {
					rules: [{
						required: true,
						errorMessage: '身份证不能为空',
					}]
				},
				nickname: {
					rules: [{
						required: true,
						errorMessage: '昵称不能为空',
					}]
				},
				phone: {
					rules: [{
						required: true,
						errorMessage: '电话不能为空',
					}]
				}
			},
		}
	},
	onLoad() {
		var that = this;
		//获取用户
		that.getUser();
	},
	methods: {

		//获取用户
		async getUser() {
			var that = this;
			var data = {};
			data['openid'] = Cache.get('user_openid');
			hitmatchcommon.getUserLibMethod(data).then(res => {
				if (res.code != 10000) {
					that.valiFormData.nickname = res.nickname;
					that.valiFormData.age = res.age;
					that.valiFormData.sex = parseInt(res.gender);
					that.valiFormData.identitycard = res.usercard_id;
					that.valiFormData.name = res.username;
					that.valiFormData.phone = res.user_mobile;
					that.valiFormData.description = res.signature;
					if (res.user_img) {
						that.imageValue = [{
							url: res.user_img
						}];
					}
				}
			});
		},
		// 获取上传状态
		select(e) {
			console.log('选择文件：', e)
			var that = this;
			that.tempFilePaths = e.tempFilePaths; //临时文件
		},
		// 获取上传进度
		progress(e) {
			console.log('上传进度：', e)
		},

		// 上传成功
		success(e) {
			console.log('上传成功')
		},

		// 上传失败
		fail(e) {
			console.log('上传失败：', e)
		},
		//電話
		async numberDecryption(e) {
			var that = this;
			var code = await Login.getcode();
			//传参
			var data = {};
			data['code'] = code;
			var backmsg = await hitmatchcommon.getJscodenApiMethod(data);
			//传参
			var data = {};
			var seesionkey = backmsg.session_key;
			data['sessionKey'] = encodeURIComponent(seesionkey);
			data['encryptedData'] = encodeURIComponent(e.encryptedData);
			data['iv'] = encodeURIComponent(e.iv);
			hitmatchcommon.getUserPhoneApiMethod(data).then(res => {
				if (res) {
					that.valiFormData.phone = res.purePhoneNumber;
				}
			});
		},
		//手机 myPrizes
		getPhoneNumber(e) {
			var that = this;
			that.valiFormData.phone = "";
			//電話
			that.numberDecryption(e.detail);
		},
		//上传数据
		updata(res) {
			var that = this;
			//插入用户
			var that = this;
			var filePath = that.tempFilePaths[0]
			var data = {};
			data['openid'] = Cache.get('user_openid');
			data['username'] = res.name;
			data['user_mobile'] = res.phone;
			data['age'] = res.age;
			data['gender'] = res.sex;
			data['usercard_id'] = res.identitycard;
			data['nickname'] = res.nickname;
			data['signature'] = res.description;
			var user_id = Cache.get('user_id');
			//判断是用户否存在1
			if (user_id) {
				//修改用户
				hitmatchcommon.updataUserapiLibMethod(data, filePath = '').then(res => {});
			} else {
				//插入用户
				hitmatchcommon.inserUserLibMethod(data, filePath = '').then(res => {

				});
			}

		},
		submit(ref) {
			var that = this;
			this.$refs[ref].validate().then(res => {
				if (that.tempFilePaths[0] || that.imageValue.length > 0) {
					that.updata(res); //上传数据
				} else {
					uni.showToast({
						title: `请上传图片`
					})
				}
			}).catch(err => {
				console.log('err', err);
			})
		},
	},



}